﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLib;
using ElementLib.Interface;
using System.Windows.Forms;
using HalconDotNet;
using System.Collections;

namespace ElementLib
{
        public class LinePack : IElemPack
        {
            private Line line;
            private ROI detectROI;
            private static int[] existCount = new int[3];
            private int detectMethod;
            private string name;
            private Hashtable args;
            private bool isSelected = false;
            public delegate void SetSelectedItem(string itemName, string mode, IElemPack iep = null);
            public delegate void AddMethod(IElemPack elemPack, int aimlist);
            public bool IsDetectType()
            {
                if (detectROI == null) return false; 
                else return true ; 
            }
            public Hashtable Args
            {
                get { return args; }
                set { args = value; }
            }
            public ROI DetectROI
            {
                get { return detectROI; }
                set { if (detectROI != null) detectROI.Dispose(); detectROI = new ROI(value); }
            }
            public int DetectMethod
            {
                get { return detectMethod; }
                set { detectMethod = value; }
            }

            public string Name
            {
                 get { return name; }
                 set { name = value; }
             }
            public bool IsSelected()
            {
                 return isSelected; 
            }

            public void BeTouched(HTuple windowHandle,SetSelectedItem method)
            {
                isSelected = isSelected == false ? true : false;
                if (isSelected)
                {
                    method(name, "add", this);
                    DispROI(windowHandle, ConstValue.SELECTED_COLOR);
                
                }
                else
                {
                     method(name, "remove", null);
                     DispROI(windowHandle, ConstValue.UNSELECTED_COLOR);
                 }
            }

        
            public void Dispose()
            {

                if (line != null) line.Dispose();
                if (detectROI != null) detectROI.Dispose();
            }
            public LinePack()
            {

            }

            public double  K { get { return L.K; }}
            public LinePack(Line line, int detectMethod, Hashtable args, ROI detectROI)
            {
                this.line = new Line(line);
                this.detectMethod = detectMethod;
                this.args = new Hashtable();
                if (args != null)
                {
                    IDictionaryEnumerator ie = args.GetEnumerator();
                    while (ie.MoveNext())
                        this.args.Add(ie.Key, ie.Value);
                }
                this.detectROI = new ROI(detectROI);
            }

            public LinePack(ROI detectROI, int detectMethod, Hashtable args)
            {
                this.detectROI = new ROI(detectROI);
                this.detectMethod = detectMethod;
                this.args = new Hashtable();
                this.line = null;
               if(args !=null)
                {
                    IDictionaryEnumerator ie = args.GetEnumerator();
                    while (ie.MoveNext())
                    this.args.Add(ie.Key, ie.Value);
                }
               
            }

            public LinePack(LinePack linePack)
            {
                name = linePack.name;
                detectMethod = linePack.detectMethod;
                if(linePack.detectROI!=null) detectROI = new ROI(linePack.detectROI);
                args = new Hashtable();
                if(linePack.args !=null)
                {
                    IDictionaryEnumerator ie = linePack.args.GetEnumerator();
                    while (ie.MoveNext())
                    args.Add(ie.Key, ie.Value);
                }
                line = new Line(linePack.line);
            }

          public void Unload()
          {
            line.Dispose();
            line = null;
          }

          
          public void ApplyChange(string itemName = "")
            {
            //if (detectMethod == ConstValue.MEASURE_LINE)
            //{


            //}
            //else if (detectMethod == ConstValue.MEASURE_LINE_PEAK)
            //{
            //    name = "PEAKLINE" + existCount[1]++;
            //}
            if (L.Name == null)
            {
                L.Name = "LINE" + existCount[0]++;
            }
             name = line.Name ;
                //line.ApplyChange();
            }

             public Line Detect(HObject image, HTuple homMat2D)
            {
                HObject actualRegion;
                HOperatorSet.AffineTransRegion(detectROI.Region(), out actualRegion, homMat2D,"nearest_neighbor");
                //HOperatorSet.AffineTransContourXld(detectROI.Contours(), out actualContour, homMat2D);
                //HOperatorSet.GenRegionContourXld(actualContour, out actualRegion, "margin");
                HTuple minThreshold, maxThreshold;
                if (detectMethod == ConstValue.MEASURE_LINE)
                {
                    minThreshold = (HTuple)args[ConstValue.MIN_THRESHOLD];
                    maxThreshold = (HTuple)args[ConstValue.MAX_THRESHOLD];
                    line = Utils.Get_line_fromROI(image, actualRegion, Utils.hv_ExpDefaultWinHandle, minThreshold, maxThreshold);
                    if (line == null) { MessageBox.Show("没有检测到"); }
                    return line;
                  }
                else if (detectMethod == ConstValue.MEASURE_LINE_PEAK)
                {
                    MessageBox.Show("需要补全");
                }
                  return null;
              }

            public void Reset(int num)
            {
                for (int i = 0; i < existCount.Length; i++)
                {
                    existCount[i] = 0;
                }
            }

            public HObject Contours() { if (line == null) return null; return line.LineContours;  }
            public HObject Region() {  if (line == null) return null; return line.Region();  }
            public void DispROI(HTuple windowHandle,string color)
            {
                HOperatorSet.SetLineWidth(windowHandle, 2);
                line.DispROI(windowHandle,color);
            }
           
        

            public void Serialization()
            {

            }

            public Line L
            {
                get { return line; }
                 set { line = value; }
            }
    }
}
